<%@page import="com.nayuta.aipo.dao.JDBCUtils.ConnectionBean"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.nayuta.aipo.dao.ScheduleUtils.ScheduleBean"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Date"%>
<%@page import="com.nayuta.aipo.dao.ScheduleUtils"%>
<%@page import="com.nayuta.aipo.dao.JDBCUtils"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
<% String jspUrl = "http://ec2-54-249-205-200.ap-northeast-1.compute.amazonaws.com/AipoSchedule/scheduleResponse.jsp"; %>
<%
ConnectionBean userlist = JDBCUtils.querySql("SELECT user_id, first_name, last_name, first_name, disabled FROM turbine_user");
out.println("ユーザ選択<br />");
if (userlist.getRs() == null) {
	return;
}
while(userlist.getRs().next()) {
	if (userlist.getRs().getInt("user_id") <= 10
			|| userlist.getRs().getBoolean("disabled")) {
		continue;
	}
	out.println("<a href='" + jspUrl + "?user_id=" + userlist.getRs().getString("user_id") + "'>" + userlist.getRs().getString("last_name") + " " + userlist.getRs().getString("first_name") + "</a><br />");	
}
JDBCUtils.closeConnect(userlist);
%>
<hr>
<%
if (request.getParameter("user_id") == null) {
	return;
}
	
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.DATE, 1);
startDate.set(Calendar.HOUR, 0);
startDate.set(Calendar.MINUTE, 0);
int addMonth=0;
if (request.getParameter("add_month") != null) {
	addMonth = Integer.valueOf((String) request.getParameter("add_month"));
}
startDate.add(Calendar.MONTH, addMonth);
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate.getTime());
endDate.add(Calendar.MONTH, 1);
endDate.add(Calendar.DATE, -1);
ScheduleBean ret = ScheduleUtils.getScheduleList(Integer.valueOf((String) request.getParameter("user_id")), startDate.getTime(), endDate.getTime(), null, null);
ResultSet rs = ret.getConnectionBean().getRs();
out.println("<h2><center>");
out.println("<a href='" + jspUrl + "?user_id=" + (String) request.getParameter("user_id") + "&add_month=" + (addMonth - 1) + "'>先月</a>");
out.println(startDate.get(Calendar.YEAR) + "/" + (startDate.get(Calendar.MONTH) + 1));
out.println("<a href='" + jspUrl + "?user_id=" + (String) request.getParameter("user_id") + "&add_month=" + (addMonth + 1) + "'>翌月</a>");
out.println("</center></h2>");
out.println("<table border='1' width='100%'>");
out.println("<tr>");
out.println("<th style='width:20px;'>開始日時</th>");
out.println("<th style='width:20px;'>終了日時</th>");
out.println("<th style='width:300px;'>タイトル</th>");
out.println("<th>内容</th>");
out.println("</tr>");
if (rs == null) {
	return;
}
while(rs.next()) {
	if (rs.getInt("user_id") != Integer.valueOf((String) request.getParameter("user_id"))
			|| !rs.getString("public_flag").equals("O")) {
		continue;
	}
	out.println("<tr>");
	out.println("<td>" + new SimpleDateFormat("MM/dd kk:mm").format(rs.getTimestamp("start_date")) + "</td>");
	out.println("<td>" + new SimpleDateFormat("MM/dd kk:mm").format(rs.getTimestamp("end_date")) + "</td>");
	out.println("<td>" + rs.getString("name") + "</td>");
	out.println("<td style='word-break:break-all;'>" + rs.getString("note").replaceAll("\n", "<br />") +"</td>");

	out.println("</tr>");
}
out.println("</table>");
JDBCUtils.closeConnect(ret.getConnectionBean());
%>
</body>
</html>